﻿@page "/roles/index"
@attribute [Authorize(Policy = nameof(ApiRoutes.Role.QueryRoles))]

@inject IRoleService RoleService
@inject DialogService DialogService
@inject ExampleJsInterop JsInterop
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject IAuthorizationService AuthorizationService

<RadzenCard Variant="Variant.Filled" Style="height : 100%;">

    <RadzenStack>
        <SearchFormPartial Model="_searchRequestDto" QueryHandler="QueryHandler"></SearchFormPartial>

        <SearchTablePartial @ref="_requestTable" T="RoleInfoResponseDto" ServerData="ServerReload"
                            SearchPartial="_searchPartial" AddHandler="AddHandler"
                            EditHandler="EditHandler">
        </SearchTablePartial>
    </RadzenStack>

</RadzenCard>

@code {

    private RoleSearchRequestDto _searchRequestDto = new();
    private IEnumerable<RoleInfoResponseDto> _pagedData = new List<RoleInfoResponseDto>();
    private SearchPartialInfo _searchPartial = new();
    private SearchTablePartial<RoleInfoResponseDto> _requestTable = new();
    private int _totalItems;

    async Task QueryHandler(MouseEventArgs args)
    {
        await _requestTable.ReloadServerData();
    }

    async Task AddHandler(MouseEventArgs args)
    {
        await DialogService.OpenAsync<RoleEdit>("Add Role", null);
        await _requestTable.ReloadServerData();
    }

    async Task EditHandler(MouseEventArgs args, RoleInfoResponseDto context)
    {
        await DialogService.OpenAsync<RoleEdit>("Edit Role", new Dictionary<string, object> { { "Key", context.Key } });
        await _requestTable.ReloadServerData();
    }

    /// <summary>
    /// Here we simulate getting the paged, filtered and ordered data from the server
    /// </summary>
    async Task<ODataServiceResult<RoleInfoResponseDto>> ServerReload(LoadDataArgs state)
    {
        if (state.Skip != null) _searchRequestDto.Page = (int)(state.Skip + 1);
        if (state.Top != null) _searchRequestDto.Limit = (int)state.Top;
        var data = await RoleService.QueryRolesByPage(_searchRequestDto);

        if (data != null)
        {
            _pagedData = data.Data;
            _totalItems = data.TotalCount;
        }

        return new ODataServiceResult<RoleInfoResponseDto>() { Count = _totalItems, Value = _pagedData };
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        var authenticationState = await AuthenticationStateProvider.GetAuthenticationStateAsync();

        _searchPartial.IsAdd = (await AuthorizationService.AuthorizeAsync(authenticationState.User, nameof(ApiRoutes.Role.CreateRole))).Succeeded;
        _searchPartial.IsEdit = (await AuthorizationService.AuthorizeAsync(authenticationState.User, nameof(ApiRoutes.Role.UpdateRole))).Succeeded;
        _searchPartial.IsDelete = (await AuthorizationService.AuthorizeAsync(authenticationState.User, nameof(ApiRoutes.Role.DeleteRole))).Succeeded;
        _searchPartial.IsImportExcel = (await AuthorizationService.AuthorizeAsync(authenticationState.User, nameof(ApiRoutes.Role.ImportRole))).Succeeded;
        _searchPartial.IsExportExcel = (await AuthorizationService.AuthorizeAsync(authenticationState.User, nameof(ApiRoutes.Role.ExportRole))).Succeeded;

    }

}